//
//  PKRootViewController.m
//  PianKe
//
//  Created by ybq on 15/12/21.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKRootViewController.h"
#import "PKRootRightViewController.h"
#import "PKRadioMainViewController.h"
#import "PKReadMainViewController.h"
#import "PKLoginViewController.h"
#import "PKRootListTableViewCell.h"

@interface PKRootViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) PKRootRightViewController *rightVC;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) UINavigationController *naviController;

@end

@implementation PKRootViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    // Do any additional setup after loading the view.
    // 1.创建rightVC,把rightVC添加到根视图上面
    // 写为属性 防止ARC释放掉VC后点击按钮无效
    _rightVC = [[PKRadioMainViewController alloc] init];
    _rightVC.heading = @"电台";
    _naviController = [[UINavigationController alloc] initWithRootViewController:_rightVC];
    // 隐藏导航栏
    _naviController.navigationBar.hidden = YES;
    
    UIImage *radioImage = [UIImage imageNamed:@"radio"];
    NSDictionary *radioDic = [NSDictionary dictionaryWithObject:radioImage forKey:@"电台"];
    UIImage *readImage = [UIImage imageNamed:@"yuedu"];
    NSDictionary *readDic = [NSDictionary dictionaryWithObject:readImage forKey:@"阅读"];
    UIImage *communityImage = [UIImage imageNamed:@"shequ"];
    NSDictionary *communityDic = [NSDictionary dictionaryWithObject:communityImage forKey:@"社区"];
    UIImage *productImage = [UIImage imageNamed:@"liangpin"];
    NSDictionary *productDic = [NSDictionary dictionaryWithObject:productImage forKey:@"良品"];
    
    _dataSource = @[radioDic, readDic, communityDic, productDic];
    // 创建显示模块标题的列表
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 240, kWidth - 100, kHeight - 240 -60)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 55;
    // 隐藏cell线
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor colorWithRed:61.0 / 255 green:61.0 / 255 blue:61.0 / 255 alpha:1];
    [tableView registerClass:[PKRootListTableViewCell class] forCellReuseIdentifier:@"list"];
    
    
    UIImageView *bottomImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kHeight - 60, kWidth, 60)];
    bottomImageView.backgroundColor = [UIColor colorWithRed:28.0 / 255 green:28.0 / 255 blue:28.0 / 255 alpha:1];
    
    UIImageView *playImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
    playImageView.image = [UIImage imageNamed:@"bofangicon"];
    [bottomImageView addSubview:playImageView];
    
    UIButton *playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    playButton.frame = CGRectMake(230, 18, 25, 25);
    [playButton setImage:[UIImage imageNamed:@"bofang"] forState:(UIControlStateNormal)];
    [bottomImageView addSubview:playButton];
    
    [self addLeftView];
    [self.view addSubview:tableView];
    [self.view addSubview:bottomImageView];
    [self.view addSubview:_naviController.view];
    
}


// 侧边栏上部
- (void)addLeftView {
    
    UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 220)];
    leftView.userInteractionEnabled = YES;
    leftView.backgroundColor = [UIColor colorWithRed:61.0 / 255 green:61.0 / 255 blue:61.0 / 255 alpha:1];
    
    UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 35, 50, 50)];
    headerImageView.layer.cornerRadius = 25;
    headerImageView.layer.masksToBounds = YES;
    headerImageView.image = [UIImage imageNamed:@"touxiang"];
    [leftView addSubview:headerImageView];
    
    UILabel *loginStateLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 50, 100, 20)];
    loginStateLabel.text = @"登陆 丨 注册";
    loginStateLabel.textColor = [UIColor whiteColor];
    loginStateLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginAction:)];
    [loginStateLabel addGestureRecognizer:tap];
    [leftView addSubview:loginStateLabel];
    
    for (int i = 0; i < 4; i++) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        btn.frame = CGRectMake(25 + i * 70, 110, 18 , 18);
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",i + 1]] forState:(UIControlStateNormal)];
        [leftView addSubview:btn];
    }
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(20, 150, 230, 30)];
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    searchBar.barStyle = UIBarStyleBlackTranslucent;
    [leftView addSubview:searchBar];
    
    [self.view addSubview:leftView];
}

- (void)loginAction:(UITapGestureRecognizer *)tap {
    PKLoginViewController *loginVC = [[PKLoginViewController alloc] init];
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [self presentViewController:navC animated:YES completion:nil];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PKRootListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"list"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *cellDic = ((NSDictionary *)_dataSource[indexPath.row]);
    NSString *cellKey = cellDic.allKeys.firstObject;
    cell.titleLabel.text = cellKey;
    cell.logoImageView.image = [cellDic objectForKey:cellKey];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *classArray = @[@"PKRadioMainViewController", @"PKReadMainViewController", @"PKCommunityMainViewController",  @"PKProductMainViewController"];
    // isKindOfClass 判断对象是否属于这个类或者这个类的子类
    // isMemberOfClass 判断对象是否属于这个类
//    if ([_rightVC isMemberOfClass:NSClassFromString(classArray[indexPath.row])]) {
//        NSLog(@"重复点击");
//        return;
//    }
    [_naviController.view removeFromSuperview];

    _rightVC = [[NSClassFromString(classArray[indexPath.row]) alloc] init];
    PKRootListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    _rightVC.heading = cell.titleLabel.text;
    _naviController = [[UINavigationController alloc] initWithRootViewController:_rightVC];
    _naviController.navigationBar.hidden = YES;
    _naviController.view.frame = CGRectMake(kWidth - 100, 0, kWidth, kHeight);
    [self.view addSubview:_naviController.view];
    [UIView animateWithDuration:.5 animations:^{
        _naviController.view.frame = _rightVC.view.bounds;
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
